package com.wenbo.ocr.controller;

import com.wenbo.ocr.utils.OcrUtil;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.script.*;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author wenbo
 * @Date 2023/10/18 21:54
 **/

@RestController
@RequestMapping({"/api/mst/recode"})
public class RecodeController {
    @GetMapping({"/read"})
    public String getRecodeDate(@RequestParam("url") String url, @RequestParam("path") String path) throws FileNotFoundException {
        String code = OcrUtil.GetCodes("https://cd.bbjh.org.cn/MyCheckHandler.ashx", "D:\\codetest\\1.png");
        return code;
    }

    @GetMapping({"/GetCodes"})
    public String getRecode(@RequestParam("url") String url, @RequestParam("path") String path) throws FileNotFoundException {
        String code = OcrUtil.GetCodes(url, path);
        System.out.println(code);
        return code;
    }

    @GetMapping({"/GetCodesTes"})
    public String getRecodeTes(@RequestParam("url") String url) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("captchaphoto/code.png");
        String urls = classPathResource.getURL().getPath();
        String substring = urls.substring(1);
        String s = OcrUtil.GetCodes(url, substring);
        System.out.println(s);
        return s;
    }

    @GetMapping({"/login"})
    public void getlogin(@RequestParam("txtUser") String txtUser, @RequestParam("txtPass") String txtPass, @RequestParam("txtValidity") String txtValidity) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("https://cd.bbjh.org.cn/");
        HttpResponse httpResponse = httpClient.execute(httpGet);
        String html = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
        Document document = Jsoup.parse(html);
        Element loginForm = document.select("form[action='./Default.aspx?ReturnUrl=%2f']").first();
        String actionUrl = loginForm.attr("action");
        Elements inputElements = loginForm.select("input");
        List<NameValuePair> parameters = new ArrayList();
        Iterator var13 = inputElements.iterator();

        String inputName;
        while(var13.hasNext()) {
            Element inputElement = (Element)var13.next();
            inputName = inputElement.attr("name");
            String inputValue = inputElement.attr("value");
            if (inputName.equals("txtUser")) {
                parameters.add(new BasicNameValuePair(inputName, "lrh1"));
            }

            if (inputName.equals("txtPass")) {
                parameters.add(new BasicNameValuePair(inputName, "jmk132628"));
            }

            if (inputName.equals("txtValidity")) {
                parameters.add(new BasicNameValuePair(inputName, "2434"));
            }
        }

        System.out.println("Action URL: " + actionUrl);
        System.out.println("Parameters: " + parameters);
        HttpPost httpPost = new HttpPost("https://cd.bbjh.org.cn/" + actionUrl.replace("./", ""));
        httpPost.setEntity(new UrlEncodedFormEntity(parameters));
        HttpResponse httpResponse2 = httpClient.execute(httpPost);
        inputName = EntityUtils.toString(httpResponse2.getEntity(), "utf-8");
        System.out.println(inputName);
        ScriptEngineManager manager = new ScriptEngineManager();
        List<ScriptEngineFactory> factories = manager.getEngineFactories();
        Iterator var18 = factories.iterator();

        while(var18.hasNext()) {
            ScriptEngineFactory factory = (ScriptEngineFactory)var18.next();
            System.out.println(factory.getNames());
        }

        ScriptEngine engine = manager.getEngineByName("js");
        Invocable invocable = (Invocable)engine;
        Document doc = Jsoup.parse(inputName);
        Element tds = (Element)doc.getElementsByTag("script").get(5);
        String data = tds.toString().replace("<script type=\"text/javascript\">", "").replace("</script>", "");
        String o = null;

        try {
            engine.eval(data);
            o = (String)invocable.invokeFunction("Login", new Object[0]);
            System.out.println(o);
        } catch (NoSuchMethodException | ScriptException var25) {
            var25.printStackTrace();
        }

    }

}
